import {assign} from 'global.js';
import Hotdata from 'hotdata.js';
class user{
    id = ''
    nickname = '';
    username = '';
    groupTitle = '';
    photo = '';
    
    status = ''
    currentPage = '';
    powerList  = [];
    data = {}
    super = false;

    setData(data){
        if(data){
            this.data = data;
        
            if (data.user_id)[
                this.id = data.user_id
            ]

            this.nickname = data.nickname??'';
            this.username = data.username??'';
            this.groupTitle = data.group_title??'';
            this.photo = data.photo??'';
            this.status = data.status??'';
            
            if (data.power!='ALL' && data.power !=  null){
                this.powerList = data.power.split(',');
                for(let i=0;i<this.powerList.length;i++){
                    this.powerList[i] = this.powerList[i].toLowerCase();
                }
            }

            if (data.power=='ALL'){
                this.super = true;
            }
        }
    }

    power(name){

        if (this.super) return true;
        
        name = name.toLowerCase();
        for(let i=0;i<this.powerList.length;i++){
            if(this.powerList[i].substr(0,name.length)==name){
                return true;
            }
        }

        return false;
    }

    getStorageName(key){
        return  'adminuser_'+this.id+'_'+key;
    }

    getLocalStorage(key,refresh = true){

        if (refresh){
            if (top.__admin_refresh_list ==  undefined) top.__admin_refresh_list = [];
            
            if (top.__admin_refresh_list.indexOf(key) == -1){
                top.__admin_refresh_list.push(key);
            };
           
        }

        let str = localStorage.getItem(this.getStorageName(key));
        if(str){
           let  data =  JSON.parse(str);

           if (data.time && data.time!=0 && data.time < new Date().getTime()){
               localStorage.removeItem(this.getStorageName(key));
               return null;
           }
           if (data.value) return data.value;
        }
        
        return null;
    }

    setLocalStorage(key,data,expire = 0){
        
        let save = {
            time: expire==0?0:new Date().getTime() + expire*1000,
            value: data
        }

        localStorage.setItem(this.getStorageName(key),JSON.stringify(save));
    }


    delLocalStorage(key){
        localStorage.removeItem(this.getStorageName(key));
    }

    clearLocalStorage(){
        for(let i=0;i<localStorage.length;i++){
            let key = localStorage.key(i);
            if(key.indexOf(this.getStorageName(''))==0){
                localStorage.removeItem(key);
            }
        }
    }

    initPage(data){
        
        let old = sessionStorage.getItem('admin_'+page);
        let back = sessionStorage.getItem('admin_back');

        if (old && back == 'back'){
            data = assign(data,JSON.parse(old));
        }

        return data;
    }

    setPage(data){
        sessionStorage.setItem('admin_'+page,JSON.stringify(data));
    }

    setBackStatus(){
        sessionStorage.setItem('admin_back',this.currentPage);
    }

    addFlash(key){
        if (top.__admin_frash_list == undefined) top.__admin_frash_list = [];

        if (top.__admin_frash_list.indexOf(key) == -1){
            top.__admin_frash_list.push(key);
        }
    }

    hotdata(name,primarykey,size = 100){
        return new Hotdata(this.getStorageName(name),primarykey,size);
    }
}

export default user;